home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / Lex.cpt / Lex / CLEX.LXI < prev    next >
Text File  |  1990-04-21  |  4KB  |  182 lines

  1. /*
  2.  * C lex
  3.  */
  4. %{
  5. #include    <stdlib.h>
  6. #include    <console.h>
  7. extern    char    *install();
  8. char    *yyval;
  9. %}
  10. digit    = [0-9];
  11. letter    = [a-zA-Z_];
  12. name    = letter(letter|digit)*;
  13. integer = digit digit*;
  14. %{
  15. main(int argc, char **argv)
  16. {
  17.     register int    i;
  18.     char            buffer[80];
  19.     extern char        *token();
  20.     extern FILE *    lexin;
  21. #ifdef    THINK_C
  22.     console_options.nrows = 20;
  23.     cecho2file("clex log", 0, stdout);
  24.     cecho2file("clex log", 1, stderr);
  25. /*    argc = ccommand(&argv); */
  26.     lexin = fopen("test.c", "r");
  27.     if(lexin == NULL)
  28.         Debugger();
  29. #endif
  30.  
  31.     while (i = yylex()) {
  32.         gettoken(buffer, sizeof (buffer));
  33.         printf("line %2d:yylex    returns %3d, token = \"%s\"\n",
  34.                 yyline, i, *buffer == '\n' ? "\n" : buffer);
  35.         if (i == LEXERR) {
  36.             error("LEXERR -- abort");
  37.             break;
  38.         }
  39.     }
  40. }
  41. %}
  42. %%
  43. %{
  44.         register int    c;
  45. %}
  46. #DEFINE        {            return(__na__); }
  47. #ELSE        {            return(__na__); }
  48. #ELIF        {            return(__na__); }
  49. #ENDIF        {            return(__na__); }
  50. #IFDEF        {            return(__na__); }
  51. #IFNDEF        {            return(__na__); }
  52. #LINE        {
  53.             comment("\n");
  54.             return(LEXSKIP);
  55.             }
  56. #INCLUDE    {            return(__na__); }
  57. STRUCT        {            return(__na__); }    /* was: return (-1) */
  58. TYPEDEF        {            return(__na__); }    /* was: return (-1) */
  59. AUTO        {            return(__na__); }
  60. EXTERN        {            return(__na__); }
  61. STATIC        {            return(__na__); }
  62. REGISTER    {            return(__na__); }
  63. GOTO        {            return(__na__); }
  64. RETURN        {            return(__na__); }
  65. IF            {            return(__na__); }
  66. WHILE        {            return(__na__); }
  67. ELSE        {            return(__na__); }
  68. SWITCH        {            return(__na__); }
  69. CASE        {            return(__na__); }
  70. BREAK        {            return(__na__); }
  71. CONTINUE    {            return(__na__); }
  72. DO            {            return(__na__); }
  73. DEFAULT        {            return(__na__); }
  74. FOR            {            return(__na__); }
  75. SIZEOF        {            return(__na__); }
  76. TYPEDEF        {            return(__na__); }
  77. UNION        {            return(__na__); }
  78. INT            {            return(__na__); }
  79. CHAR        {            return(__na__); }
  80. FLOAT        {            return(__na__); }
  81. LONG        {            return(__na__); }
  82. SHORT        {            return(__na__); }
  83. name        {
  84.             printf("\nmatched name:");
  85.             yyval = install();
  86.             return(6);
  87.             }
  88. integer        {
  89.             printf("\nmatched integer:");
  90.             yyval = install();
  91.             return(7);
  92.             }
  93. "<"            {            return(__na__); }
  94. "<="        {            return(__na__); }
  95. "="            {            return(__na__); }
  96. "!="        {            return(__na__); }
  97. ">="        {            return(__na__); }
  98. ">"            {            return(__na__); }
  99. "<<"        {            return(__na__); }
  100. ">>"        {            return(__na__); }
  101. "=+"        {            return(__na__); }
  102. "=-"        {            return(__na__); }
  103. "=/"        {            return(__na__); }
  104. "=%"        {            return(__na__); }
  105. "%"            {            return(__na__); }
  106. "/"            {            return(__na__); }
  107. "*"            {            return(__na__); }
  108. "=*"        {            return(__na__); }
  109. "=<<"        {            return(__na__); }
  110. "=>>"        {            return(__na__); }
  111. "&"            {            return(__na__); }
  112. "|"            {            return(__na__); }
  113. "=|"        {            return(__na__); }
  114. "=&"        {            return(__na__); }
  115. "+"            {            return(__na__); }
  116. "-"            {            return(__na__); }
  117. "++"        {            return(__na__); }
  118. "--"        {            return(__na__); }
  119. ";"            {            return(__na__); }
  120. "?"            {            return(__na__); }
  121. "."            {            return(__na__); }
  122. ","            {            return(__na__); }
  123. "/*"        {
  124.             comment("*/");
  125.             return(LEXSKIP);
  126.             }
  127. "'"            {
  128.             if ((c = mapch('\'', '\\')) != -1)
  129.                     while (mapch('\'', '\\') != -1)
  130.                             lexerror("Long character constant");
  131.             printf("%c", c);
  132.             return(__na__);
  133.             }
  134. "\""        {            return(__na__); }        /* This should call "lexswitch" */
  135. "\n"        {            return(__na__); }
  136. " "            {            return(__na__); }
  137. "\t"        {            return(__na__); }
  138. "||"        {            return(__na__); }
  139. "&&"        {            return(__na__); }
  140. "("            {            return(__na__); }
  141. ")"            {            return(__na__); }
  142. "["            {            return(__na__); }
  143. "]"            {            return(__na__); }
  144. "{"            {            return(__na__); }
  145. "}"            {            return(__na__); }
  146. %%
  147. char *
  148. install()
  149. /*
  150.  * Install the current token in the symbol table
  151.  */
  152.  
  153. {
  154.     register char    *buffer;        /* Where to put the character    */
  155.     register char    *first;            /* -> first byte of the token    */
  156.     char            *last;            /* Can't be in a register        */
  157.     extern char        *token();
  158.     extern char     *copy();
  159.     
  160.     first = token(&last);            /* Find first/last of token        */
  161.     printf("\tinstalling :%s", first);
  162.     if ((buffer = calloc((last - first) + 1, sizeof(char))) == NULL) {
  163.         error("Out of space in install");
  164.         exit(1);
  165.     }
  166.     first = copy(buffer, first, (int)(last - first));
  167.     *first = '\0';
  168.     return(buffer);
  169. }
  170.  
  171. error(char *s) {
  172.     fprintf(stderr, s);
  173.     exit(1);
  174.     }
  175.  
  176. char *
  177. copy(char *to, char *from, int len) {
  178.     memcpy(to,from,len);
  179.     return(&to[len + 1]);
  180.     }
  181.  
  182. void yyinit() {};